package org.example.service.imp;

import org.apache.ibatis.session.SqlSession;
import org.example.bean.RecordBean;
import org.example.mapper.IRecordMapper;
import org.example.service.IRecordService;

import java.util.List;

public class RecordServiceImp extends BaseService implements IRecordService{

    @Override
    public List<RecordBean> findByRoomID(Integer roomID) {
        SqlSession session = this.getSession();
        IRecordMapper mapper = session.getMapper(IRecordMapper.class);
        List<RecordBean> list = mapper.findByRoomID(roomID);
        session.close();
        return list;
    }

    @Override
    public void add(RecordBean recordBean, Integer roomID) {
        SqlSession session = this.getSession();
        IRecordMapper mapper = session.getMapper(IRecordMapper.class);
        mapper.add(recordBean,roomID);
        mapper.updateRoomStatusToBad(roomID);
        session.commit();
        session.close();
    }

    @Override
    public void update(Integer recordID, Integer roomID) {
        SqlSession session = this.getSession();
        IRecordMapper mapper = session.getMapper(IRecordMapper.class);
        mapper.update(recordID,roomID);
        List<RecordBean> list = mapper.findBadRecord(roomID);
        System.out.println(list.size());
        if (list.size()==0){
            System.out.println("=============================================================");
            mapper.updateRoomStatusToGood(roomID);
        }
        session.commit();
        session.close();
    }
}
